from map import Map
from planner import Planner


class Robot:
	planner = Planner(True)
	def __init__(self,wallFollow):
		self.map = Map('robot')
		self.history = []
		self.planner = Planner(wallFollow)

	def updateMap(self, observs):
		for o in observs:
			assert len(o) == 3
			x = o[0]
			y = o[1]
			observation = o[2]
			# print x, y
			assert observation == 0 or observation == -1  # observation can only be 0 or -1
			assert not (x < 0 or x >= self.map.row_size or y < 0 or y >= self.map.col_size)
			# assume obstacles don't move. Hence only update unknown cells
			if self.map.map[x][y] == -2:  # unknown at the moment
				self.map.map[x][y] = observation

	def getNextAction(self):
		return self.planner.getNextAction(self.map)


	def executeActionSequence(self, actions):
		dx, dy = self.map.getDirArrayBasedOnOrientation()
		x = self.map.robotX
		y = self.map.robotY
		for action in actions:
			assert action in range(4)
			x = x + dx[action]
			y = y + dy[action]
			assert not (x < 0 or x >= self.map.row_size or y < 0 or y >= self.map.col_size)
			assert not (
				self.map.map[x][y] == -2 or self.map.map[x][y] == -1)  # the new location cannot be an obstacle or unknown
			self.map.map[x][y] += 1  # increment the number of visits to cell x,y

		self.map.robotX = x
		self.map.robotY = y
		self.history.append([self.map.robotO, actions])
		self.map.robotO = (self.map.robotO + actions[len(actions) - 1]) % 4
		return self.map.robotX, self.map.robotY, self.map.robotO



